$(function () {
  const  layer  = layui.layer;
  $("#logout").on('click',function (e) {
    e.preventDefault();
    console.log('xxxx');
    
    layer.confirm("是否退出登录？", { icon: 3, title: "提示" }, function (index) {
      //do something
      localStorage.removeItem("token");

      location.href = "./login.html";

      layer.close(index);
    });
  });
  getInfo();
});

function getInfo() {
  // 获取用户信息
  $.ajax({
    type: "get",
    url: "/my/userinfo",
    success: function (res) {
      if (res.status !== 0) return layer.msg(res.msg);
      randerInfo(res.data);
    }
  });

  function randerInfo(data) {
    // email: ""  id: 2548  nickname: ""  user_pic: null username: "song"

    const myname = data.username || data.nickname;
    $(".username").text(myname);
    const firstName = myname.charAt(0).toUpperCase();
    $(".text-avatar").text(firstName);
    if (data.user_pic) {
      $(".userinfo img").attr("src", data.user_pic);
      $(".userinfo .text-avatar").hide();
    } else {
      $(".userinfo img").hide();
    }
  }
}
